//
//  ClassCollectionView.m
//  MyItem
//
//  Created by SoulWater on 15/12/26.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "ClassCollectionView.h"
#import "ClassCollectionViewCell.h"
#import "detailWareModel.h"
#import "ClassificationViewController.h"
#import "WareListViewController.h"
@implementation ClassCollectionView
{
    NSArray *_urlArray;
    NSArray *_bodyArray;
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self _createView];
    }
    return self;
}

-(void)_createView
{
    self.dataSource = self;
    self.delegate = self;
    
    //注册单元格
    [self registerNib:[UINib nibWithNibName:@"ClassCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collectionViewCell"];
   
    //注册头视图
    [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
               withReuseIdentifier:@"headView"];
    
    [self loadArray];
}


#pragma mark - delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.nameArray.count;
}


//头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headView" forIndexPath:indexPath];

        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, HScreenWidth/4*3, 70)];
    
        NSString *imgStr = [self.headViewUrl substringToIndex:self.headViewUrl.length-5];
//        NSLog(@"%@",imgStr);
        [imgView sd_setImageWithURL:[NSURL URLWithString:imgStr]];
        
        [self addSubview:imgView];



        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 75, HScreenWidth/4*3, 20)];
        label.text = self.nameArray[indexPath.section];
        
        [headView addSubview:label];

        
        return headView;
    }
    else
    {
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headView" forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, HScreenWidth/4*3, 30)];
        label.text = self.nameArray[indexPath.section];
        
        [headView addSubview:label];
        
        return headView;
    }
    return nil;
}

//单元格数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    
    cell.wareModel = _dataArray[indexPath.row];
    
    return cell;
}

//选中单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WareListViewController *wareVC = [[WareListViewController alloc]init];
    ClassificationViewController *mainVC = (ClassificationViewController *)self.viewController;
    [mainVC.navigationController pushViewController:wareVC animated:YES];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager POST:_urlArray[0] parameters:_bodyArray[0] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
//        NSLog(@"%@",responseObject);
        
        NSArray *wareInfo = responseObject[@"wareInfo"];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *dic in wareInfo)
        {
            detailWareModel *model = [[detailWareModel alloc]initWithDataDic:dic];
            
            [array addObject:model];
        }
    
        wareVC.dataArray = array;

        [wareVC.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
}




-(void)loadArray
{
    _urlArray = @[
                  @"http://search.m.jd.com/client.action?functionId=searchCatelogy",
                  @"http://search.m.jd.com/client.action?functionId=search",
                  @"http://search.m.jd.com/client.action?functionId=search"
                  ];
    
    _bodyArray = @[
                   @{
                       @"sign": @"3wQFUOfdUAXjiuiuUWLL8A",
                       @"d_model": @"iPhone4,1",
                       @"networkType": @"wifi",
                       @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                       @"st": @"1451293361165",
                       @"body": @"{\"pagesize\":\"10\",\"stock\":\"1\",\"newVersion\":\"3\",\"page\":\"1\",\"catelogyId\":\"3394\",\"catelogyIdLevel1\":\"0\",\"sort\":\"1\",\"jshop\":\"1\",\"cid\":\"3394\"}",
                       @"osVersion": @"7.1.2",
                       @"d_brand": @"apple",
                       @"client": @"apple",
                       @"screen": @"640*960",
                       @"sv": @"1",
                       @"area": @"15_1213_3038_0",
                       @"build": @"35977",
                       @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                       @"clientVersion": @"4.4.3",
                       @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                       @"partner": @"apple"
                    },
                   @{
                       @"sign": @"PPbxfpZbU4nQTG6KUvIFbw",
                       @"d_model": @"iPhone4,1",
                       @"networkType": @"wifi",
                       @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                       @"st": @"1451304275494",
                       @"body": @"{\"pagesize\":\"10\",\"stock\":\"1\",\"isCorrect\":\"1\",\"page\":\"1\",\"newVersion\":\"3\",\"jshop\":\"1\",\"keyword\":\"休闲男鞋\"}",
                       @"osVersion": @"7.1.2",
                       @"d_brand": @"apple",
                       @"client": @"apple",
                       @"screen": @"640*960",
                       @"sv": @"1",
                       @"area": @"15_1213_3038_0",
                       @"build": @"35977",
                       @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                       @"clientVersion": @"4.4.3",
                       @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                       @"partner": @"apple"
                    },
                   @{
                       @"sign": @"3yrWjulX9Dks50qXTVx8Hg",
                       @"d_model": @"iPhone4,1",
                       @"networkType": @"wifi",
                       @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                       @"st": @"1451304564845",
                       @"body": @"{\"pagesize\":\"10\",\"stock\":\"1\",\"isCorrect\":\"1\",\"page\":\"1\",\"newVersion\":\"3\",\"jshop\":\"1\",\"keyword\":\"安全套\"}",
                       @"osVersion": @"7.1.2",
                       @"d_brand": @"apple",
                       @"client": @"apple",
                       @"screen": @"640*960",
                       @"sv": @"1",
                       @"area": @"15_1213_3038_0",
                       @"build": @"35977",
                       @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                       @"clientVersion": @"4.4.3",
                       @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                       @"partner": @"apple"
                    }
                   ];
}
@end
